<?php


namespace App\Http\Middleware;


use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class AccessLog
{

    public function handle(Request $request, Closure $next)
    {
        Log::info('Request Information', [
            'method'=>$request->getMethod(),
            'uri' => $request->getRequestUri(),
            'token'=>$_SERVER['HTTP_AUTHORIZATION']??"",
            'all' => $request->input(),
            'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
            'ua' => $_SERVER['HTTP_USER_AGENT'] ?? '',
            'cookie' => $_SERVER['HTTP_COOKIE'] ?? '',
        ]);
        $response = $next($request);

        // 在响应发送之后记录响应信息
        $response->macro('logResponse', function () use ($response) {
            Log::info('Response Information', [
                'status' => $response->getStatusCode(),
                'content' => $response->getContent()
            ]);
        });

        $response->logResponse();

        return $response;    }
}
